package com.gable.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.Callback;
import com.aliyun.oss.model.Callback.CalbackBodyType;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

/**
 * 此类是阿里云上传的辅助类
 * 
 * @author jacky
 *
 */
public class FileUploadUtil {
		
	
	//阿里获取文件的域名  (域名/文件路径/加key通常key就是文件名)
	public static final String REALMNAME = "http://exhibition0301.oss-cn-shanghai.aliyuncs.com";
	//阿里服务器上传文件的服务器
	private static final String ENDPOINT="http://oss-cn-shanghai.aliyuncs.com";
	//连接服务器的keyId
	private static final String ACCESSKEYID="LTAIYokZqmBOpCTo";
	//连接服务器的KeySecret
	private static final String ACCESSKEYSECRET="W80vFHHZDYvxeSXhEgIqoWK0xquUNj";
	//在阿里云的项目文件夹，通常是根目录
	public static final String BUCKETNAME="exhibition0301";
	//上传成功后回调路径
	public static final String callbackUrl = "<yourCallbackServerUrl>";
	
	public static OSSClient ossClient = null;
	
	/**
	 * 上传图片
	 * @return
	 */
	public static boolean fileUpload(byte [] bytes,String key){
		OSSClient ossClient = getOssClient();
		ByteArrayInputStream  byteArrayInputStream= null;
		try {
			boolean bl = ossClient.doesBucketExist(BUCKETNAME);
			if(!bl){
				ossClient.createBucket(BUCKETNAME);
			}
			byteArrayInputStream= new ByteArrayInputStream(bytes);
			ossClient.putObject(BUCKETNAME,key,byteArrayInputStream);
			
			if(ExistObject(key)){
				return true;
			}
			
			} catch (Exception e){
				e.printStackTrace();
			}finally {
				try {
					byteArrayInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				ossClient.shutdown();
			}
			
			return false;
	}
	
	/**
	 * 判断当前文件是否存在
	 * @param key
	 * @return
	 */
	public static boolean ExistObject(String key){
		boolean bl = true;
		ossClient = getOssClient();
		ossClient.doesObjectExist(BUCKETNAME, key);
		ossClient.shutdown();
		return bl;
	}
	
	/**
	 * 实例化上传对象
	 * @return
	 */
	public static OSSClient  getOssClient(){
			ossClient = new OSSClient(ENDPOINT,ACCESSKEYID,ACCESSKEYSECRET);
		return ossClient;
	}
	
	/**
	 * 根据key获取文件对象
	 * @param key
	 * @return
	 * @throws IOException 
	 */
	public static OSSObject  getOSSObject (String key){
		OSSClient ossClient = getOssClient();
		OSSObject ossObject = ossClient.getObject(BUCKETNAME,key);
		ossClient.shutdown();
		return ossObject;
	}
	
	
	public static final byte[] input2byte(InputStream inStream)  
            throws IOException {  
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();  
        byte[] buff = new byte[100];  
        int rc = 0;  
        while ((rc = inStream.read(buff, 0, 100)) > 0) {  
            swapStream.write(buff, 0, rc);  
        }  
        byte[] in2b = swapStream.toByteArray();  
        return in2b;  
    }  
	
}
